<?php

return function($oRes, $db_fadm, $class_yun, $method, $username, $acctype, $amount, $mSession) {
    switch ($method) {
        case('get'):
            $mWhere = array();
            $mWhere['user_id'] = $mSession['user_id'];
            $userinfo = $db_fadm->sql()->table('system_user')->where($mWhere)->find();
            $form_item_list = array();
            $form_item_list['username'] = array('title' => '用户名', 'type' => 'text');
            $form_item_list['acctype'] = array('title' => '账户类型', 'type' => 'text', 'value' => 'basic');
            $form_item_list['amount'] = array('title' => '充值金额', 'type' => 'text', 'suffix' => '元');
            $form_item_list['balance'] = array('title' => '可充额度', 'type' => 'label', 'value' => $userinfo['balance'], 'suffix' => '元');
            $oRes->data('form_item_list', $form_item_list);
            break;
        case('post'):
            if (empty($username)) {
                $this->error('请输入用充值的用户名');
            }
            if (empty($amount)) {
                $this->error('请输入用充值的金额');
            }
            if ($amount == 0) {
                $this->error('充值金额不能为0');
            }
            $mWhere = array();
            $mWhere['username'] = $username;
            $mRow = $db_fadm->sql()->table('ntuser_user')->where($mWhere)->find();
            if (empty($mRow)) {
                $this->error("您输入的用户名[{$username}]不存在");
            }
            $ntuser_user_id = $mRow['id'];
            $db_fadm->sql()->table('ntuser_balance')->where(array('user_id' => $ntuser_user_id, 'acctype' => $acctype))->addwne(array('username' => $username, 'balance' => 0));
            $db_fadm->commit();
            $db_fadm->begin();
            $mWhere = array();
            $mWhere['user_id'] = $mSession['user_id'];
            $oSqlRgczUser = $db_fadm->sql()->table('system_user')->where($mWhere);
            $mRowFinanceRgczUser = $oSqlRgczUser->lock()->find();
            if (empty($mRowFinanceRgczUser)) {
                $this->error("您的资金获取失败");
            }
            if ($mRowFinanceRgczUser['balance'] < $amount) {
                $this->error("您的余额不足");
            }
            $oSqlRgczUser->save(array('balance' => $mRowFinanceRgczUser['balance'] - $amount));
            $title = '人工充值';
            $class_yun->NtuserBalanceLogAdd($db_fadm, 0, $ntuser_user_id, $acctype, $amount * 10000, $title);
            $db_fadm->commit();
            $oRes->assign('msg', '充值成功');
            break;
    }
};
